<?php


namespace App\Admin\Services\Wechat;


use Illuminate\Http\Request;

class MiniProgramWechatTemplateService {

    /**
     * 微信代码模板分页列表
     *
     * @param Request $request
     * @return array
     */
    public function page(Request $request) {
        $pageInfo           = $request->post('page_info');
        $miniProgramTool    = new CompanyMiniProgramService();
        $wechatTemplateList = $miniProgramTool->getTemplateList();
        $result             = [
            'total_count' => 0,
            'list'        => []
        ];
        if (!empty($wechatTemplateList)) {
            $draftCount = count($wechatTemplateList);
            //根据创建时间降序排序
            foreach ($wechatTemplateList as &$val) {
                $edition[]          = $val['create_time'];
                $val['create_time'] = date('Y-m-d H:i:s', $val['create_time']);
            }
            array_multisort($edition, SORT_DESC, $wechatTemplateList);
            //分页
            $draftListChunk = array_chunk($wechatTemplateList, $pageInfo['per_page']);
            $result         = [
                'total_count' => $draftCount,
                'list'        => $draftListChunk[$pageInfo['page'] - 1]
            ];
        }
        return $result;
    }

    /**
     * 删除微信代码模板
     *
     * @param $id
     * @return mixed
     * @throws \App\Admin\Exceptions\Wechat\CompanyMiniProgramException
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     */
    public function delete($id) {
        $miniProgramTool = new CompanyMiniProgramService();
        return $miniProgramTool->deleteTemplate($id);
    }
}